<?php
/**
 * @author  web_dxj@163.com
 * @time    2015-10-10 12:39
 */

class Category extends MY_Controller {

    public function index() {

        $this->load->model('categorys_model','categorys');
        $data['category_array'] = $this->categorys
                                       ->handler()
                                       ->get_category();
        $this->load->view('category/index',$data);
    }

    public function add() {

        $this->load->library('form_validation');
        // Form verify config
        $config = array(
            array(
                'field' => 'category_name',
                'label' => '分类名称',
                'rules' => 'required',
                'errors' => array(
                    'required' => '%s 不能为空.',
                ),
            ),
        );

        $this->form_validation->set_rules($config);
        if ( FALSE === $this->form_validation->run() )
        {
            ajax_return(self::HTTP_STATUS_FAIL,strip_tags(validation_errors()));
        }

        $data['category_name']  = $this->input->post('category_name');
        $data['category_alias'] = $this->input->post('category_alias');
        $data['parent']         = $this->input->post('parent');

        if (empty($data['category_alias']))
        {
            // 中文转拼音
            $this->load->helpers('http');
            $response = json_decode(go_curl('http://string2pinyin.sinaapp.com/','GET',array('str'=>$data['category_name'],'accent'=>0,'delimiter'=>'')));
            if ( 'T' === $response->status )
            {
                $data['category_alias'] = $response->pinyin;
            }
        }

        $this->load->model('categorys_model','categorys');
        $result = $this->categorys->insert_category($data);
        if ( TRUE === $result )
        {
            ajax_return(self::HTTP_STATUS_SUCCESS,'添加成功');
        }

        ajax_return(self::HTTP_STATUS_FAIL,'添加失败');
    }


    public function edit($id) {

        $id = hashids_decode($id);

        $this->load->model('categorys_model','categorys');
        $data['category'] = $this->categorys->find('ID',$id);

        if ( is_null($data['category']) )
        {
            show_error("分类目录不存在");
        }

        $data['category_array'] = $this->categorys->get_category();

        $this->load->view('category/edit',$data);
    }

    public function edit_post() {

        $this->load->library('form_validation');
        // Form verify config
        $config = array(
            array(
                'field' => 'category_name',
                'label' => '分类名称',
                'rules' => 'required',
                'errors' => array(
                    'required' => '%s 不能为空.',
                ),
            ),
        );

        $this->form_validation->set_rules($config);
        if ( FALSE === $this->form_validation->run() )
        {
            ajax_return(self::HTTP_STATUS_FAIL,strip_tags(validation_errors()));
        }

        $data['ID']             = $this->input->post('id');
        $data['category_name']  = $this->input->post('category_name');
        $data['category_alias'] = $this->input->post('category_alias');
        $data['parent']         = $this->input->post('parent');

        if (empty($data['category_alias']))
        {
            // 中文转拼音
            $this->load->helpers('http');
            $response = json_decode(go_curl('http://string2pinyin.sinaapp.com/','GET',array('str'=>$data['category_name'],'accent'=>0,'delimiter'=>'')));
            if ( 'T' === $response->status )
            {
                $data['category_alias'] = $response->pinyin;
            }
        }

        $this->load->model('categorys_model','categorys');
        $result = $this->categorys->update_category($data);

        if ( TRUE === $result )
        {
            ajax_return(self::HTTP_STATUS_SUCCESS,'修改成功');
        }

        ajax_return(self::HTTP_STATUS_FAIL,'修改失败');
    }


    public function del() {

        $ids_string = $this->input->post('ids');

        if ( empty($ids_string) )
        {
            show_error('请传入要删除的分类的ID');
        }

        $this->load->model('categorys_model','categorys');
        if ( TRUE === $this->categorys->del_category($ids_string) )
        {
            ajax_return(self::HTTP_STATUS_SUCCESS,'删除成功');
        }

        ajax_return(self::HTTP_STATUS_FAIL,'删除失败');
    }

}